package com.example.tagsdemo.dto;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

@Data
public class ColumnDTO implements Serializable {

    private static final long serialVersionUID = 3565661543805074611L;

    private String column;
    private String type;
    private Integer length;
    private Integer point;
    private String defaultValue;
    //1不能为null 2可以为null
    private Integer  isNull;
    //1是主键 2不是主键
    private Integer isPrimaryKey;

    private String columnRemarks;

    private static final List<String> APPEND_TIMESTAMP = Lists.newArrayList();
    private static final Set<String> TIMETYPE = Sets.newHashSet();

    static {
        TIMETYPE.add("DATETIME");
        TIMETYPE.add("TIMESTAMP");

        APPEND_TIMESTAMP.add(" CURRENT_TIMESTAMP ");
        APPEND_TIMESTAMP.add(" UPDATE CURRENT_TIMESTAMP ");
        APPEND_TIMESTAMP.add(" CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ");
    }

    public String getDefaultValue() {
        if (TIMETYPE.contains(this.getType())) {
            if (StringUtils.isBlank(this.defaultValue)) {
                this.defaultValue = APPEND_TIMESTAMP.get(0) ;
            }else{
                if (StringUtils.equals("2",this.defaultValue)){
                    this.defaultValue = APPEND_TIMESTAMP.get(1);

                }else if (StringUtils.equals("12",this.defaultValue)){
                    this.defaultValue = APPEND_TIMESTAMP.get(2);
                }else{
                    this.defaultValue = APPEND_TIMESTAMP.get(0);
                }
            }
        }
        return defaultValue;
    }
}
